# -*- coding: utf-8 -*-
import scrapy
from scrapy.loader import ItemLoader
from scrapy.loader.processors import MapCompose, Join
from ken_properties.items import KenPropertiesItem


class BasicSpider(scrapy.Spider):
    name = 'basic'
    allowed_domains = ['localhost']
    start_urls = ['http://localhost:9312/properties/property_000000.html']

    def parse(self, response):
        print("====================================")
        item_loader = ItemLoader(item=KenPropertiesItem(), response=response)
        item_loader.add_xpath('title', '//*[@itemprop="name"][1]/text()',
                              MapCompose(str.strip, str.title))
        item_loader.add_xpath('price', './/*[@itemprop="price"][1]/text()',
                              MapCompose(lambda i: i.replace(',', ''), float),
                              re='[,.0-9]+')
        item_loader.add_xpath('description', '//*[@itemprop="description"][1]/text()',
                              MapCompose(str.strip), Join())
        item_loader.add_xpath('address',
                              '//*[@itemtype="http://schema.org/Place"][1]/text()',
                              MapCompose(str.strip))
        item_loader.add_xpath('image_urls', '//*[@itemprop="image"][1]/@src',
                              MapCompose(lambda i: response.urljoin(i)))

        print("====================================")
        return item_loader.load_item()
